package com.zhss.dfs.namenode.server;

import java.util.ArrayList;
import java.util.List;

/**
 * 负责管理内存中的文件目录树的核心组件
 *
 * @author 周成龙
 */
public class FSDirectory {

    /**
     *单例模式，一个系统中只能有一个目录树
     */
    private static volatile FSDirectory instance;
    public static  FSDirectory getInstance(){
        if(instance==null){
            synchronized (FSDirectory.class){
                if(instance==null){
                    instance=new FSDirectory();
                }
            }
        }
        return instance;
    }
    //文件夹目录树
    private INodeDirectory directoryTree;

    private FSDirectory() {
        this.directoryTree = new INodeDirectory("/");
    }

    /**
     * 创建目录
     *
     * @param path 目录路径
     */
    public void mkdir(String path) {
        /**
         * 比如创建一个目录 /usr/local/java/application
         * 首先将进行分词split
         * 找到父目录插进去
         */
        synchronized (directoryTree){
            if (path == null || path.trim().length() == 0) {
                return;
            }
            INodeDirectory parentDirectory=this.directoryTree;
            String[] split = path.split("/");
            for(String directoryName:split){
                INodeDirectory iNodeDirectory=findDirectory(parentDirectory,directoryName);
                if(iNodeDirectory==null){
                    INodeDirectory node = new INodeDirectory(directoryName);
                    parentDirectory.getChildren().add(node);
                    parentDirectory=node;
                    continue;
                }
                parentDirectory=iNodeDirectory;
            }
        }

    }

    private INodeDirectory findDirectory(INodeDirectory parentDirectory, String directoryName) {
        if(parentDirectory.getChildren().size()==0){
            return null;
        }
        for (INode child : parentDirectory.getChildren()) {
            if(child instanceof INodeDirectory){
                if(((INodeDirectory) child).path.equals(directoryName)){
                    return (INodeDirectory) child;
                }
            }
        }
        return null;
    }

    /**
     * 这个接口用来统一文件和文件夹的处理
     */
    private interface INode {

    }

    /**
     * 文件夹
     */
    private class INodeDirectory implements INode {
        //文件夹名称
        private String path;
        //文件夹中包含的文件
        private List<INode> children;

        public INodeDirectory(String path) {
            this.path = path;
            children=new ArrayList<INode>();
        }

        /**
         * 获取文件或文件夹路径
         *
         * @return
         */
        public String getPath() {
            return path;
        }

        /**
         * 设置路径
         *
         * @param path
         */
        public void setPath(String path) {
            this.path = path;
        }

        /**
         * 获取文件夹下的所有文件
         *
         * @return
         */
        public List<INode> getChildren() {
            return children;
        }

        /**
         * 设置文件中的内容
         *
         * @param children
         */
        public void setChildren(List<INode> children) {
            this.children = children;
        }

        @Override
        public String toString() {
            return "INodeDirectory{" +
                    "path='" + path + '\'' +
                    ", children=" + children +
                    '}';
        }
    }

    /**
     * 文件
     */
    private class INodeFile implements INode {
        private String fileName;

        public String getFileName() {
            return fileName;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }
    }

    @Override
    public String toString() {
        return "FSDirectory{" +
                "directoryTree=" + directoryTree +
                '}';
    }
}
